#include <cstdio>
#include <algorithm>
#include <cstring>
typedef long long ll;
using namespace std;
const int N=1005;
ll a[N][N];
ll sum[N][N];
int main(void){
    int t;
    int m,n,x,y;
    scanf("%d",&t);
    while(t--){
        //矩阵前缀和
        ll Max=0;
        scanf("%d%d%d%d",&m,&n,&x,&y);
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                scanf("%lld",&a[i][j]);
                a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
                if(i>=x && j>=y){
                    Max=max(Max,a[i][j]-a[i][j-y]-a[i-x][j]+a[i-x][j-y]);
                }
            }
        }
        printf("%lld\n",Max);
    }
    return 0;
}